package com.softeem.ossmusic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.ossmusic.comm.R;
import com.softeem.ossmusic.entity.MusicListDetails;
import com.softeem.ossmusic.service.MusicListDetailsService;
import com.softeem.ossmusic.service.MusicListService;
import com.softeem.ossmusic.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import com.softeem.ossmusic.entity.Music;

@Tag(name = "歌单详情管理")
@RestController
public class MusicListDetailsController {

    @Autowired
    private MusicListDetailsService musicListDetailsService;
    
    @Autowired
    private MusicListService musicListService;
    
    @Autowired
    private MusicService musicService;

    @Operation(summary = "歌单详情列表", description = "分页查询歌单详情列表")
    @Parameters({
            @Parameter(name = "page",description = "页码"),
            @Parameter(name = "limit",description = "数据条数")
    })
    @GetMapping("/musicListDetails/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        //创建分页对象
        IPage<MusicListDetails> ipage = new Page<>(page, limit);
        //执行查询(分页)
        musicListDetailsService.page(ipage);
        //返回结果
        return R.success().setData(ipage.getRecords()).setCount(ipage.getTotal());
    }
    
    @Operation(summary = "获取歌单中的歌曲", description = "根据歌单id查询歌单中的所有歌曲")
    @GetMapping("/musicListDetails/getByMusicListId")
    public R getByMusicListId(Integer id){
        Integer musicListId = id;
        if(musicListId == null || musicListService.getById(musicListId) == null){
            return R.error().setMsg("歌单不存在");
        }
        
        // 查询歌单详情记录
        List<MusicListDetails> list = musicListDetailsService.list(
            new QueryWrapper<MusicListDetails>().eq("list_id", musicListId)
        );
        
        // 封装完整的歌曲信息
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (MusicListDetails details : list) {
            Music music = musicService.getById(details.getMusicId());
            if (music != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", details.getId());
                map.put("musicName", music.getName());
                map.put("singer", music.getSinger());
                map.put("duration", music.getDuration());
                map.put("url", music.getUrl());
                map.put("albumId", music.getAid());
                resultList.add(map);
            }
        }
        
        return R.success().setData(resultList);
    }
    
    @Operation(summary = "添加歌曲到歌单", description = "将歌曲添加到歌单中")
    @PostMapping("/musicListDetails/add")
    public R add(@RequestBody MusicListDetails details){
        //验证歌单是否存在
        if(details.getListId() == null || musicListService.getById(details.getListId()) == null){
            return R.error().setMsg("歌单不存在");
        }
        //验证歌曲是否存在
        if(details.getMusicId() == null || musicService.getById(details.getMusicId()) == null){
            return R.error().setMsg("歌曲不存在");
        }
        //检查歌曲是否已在歌单中
        QueryWrapper<MusicListDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", details.getListId())
                   .eq("music_id", details.getMusicId());
        if(musicListDetailsService.exists(queryWrapper)){
            return R.error().setMsg("歌曲已在歌单中");
        }
        
        if(musicListDetailsService.save(details)){
            return R.success().setMsg("添加成功");
        }
        return R.error().setMsg("添加失败");
    }
    
    @Operation(summary = "从歌单中删除歌曲", description = "根据歌单详情ID删除关联")
    @PostMapping("/musicListDetails/delete")
    public R deleteByDetailId(@RequestParam("id") Integer detailId){
        if(detailId == null){
            return R.error().setMsg("详情ID不能为空");
        }
        
        if(musicListDetailsService.removeById(detailId)){
            return R.success().setMsg("删除成功");
        }
        return R.error().setMsg("删除失败");
    }
    
    @Operation(summary = "从歌单中删除歌曲", description = "根据歌单id和歌曲id删除关联")
    @DeleteMapping("/musicListDetails/delete")
    public R delete(Integer musicListId, Integer musicId){
        if(musicListId == null || musicId == null){
            return R.error().setMsg("歌单ID和歌曲ID不能为空");
        }
        
        QueryWrapper<MusicListDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", musicListId)
                   .eq("music_id", musicId);
        
        if(musicListDetailsService.remove(queryWrapper)){
            return R.success().setMsg("删除成功");
        }
        return R.error().setMsg("删除失败");
    }
    
    @Operation(summary = "清空歌单", description = "清空指定歌单中的所有歌曲")
    @DeleteMapping("/musicListDetails/clear")
    public R clear(Integer musicListId){
        if(musicListId == null || musicListService.getById(musicListId) == null){
            return R.error().setMsg("歌单不存在");
        }
        
        QueryWrapper<MusicListDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", musicListId);
        
        musicListDetailsService.remove(queryWrapper);
        return R.success().setMsg("歌单已清空");
    }
    
    @Operation(summary = "检查歌曲是否在歌单中", description = "检查指定歌曲是否已在指定歌单中")
    @GetMapping("/musicListDetails/check")
    public R check(Integer musicListId, Integer musicId){
        if(musicListId == null || musicId == null){
            return R.error().setMsg("歌单ID和歌曲ID不能为空");
        }
        
        QueryWrapper<MusicListDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("list_id", musicListId)
                   .eq("music_id", musicId);
        
        boolean exists = musicListDetailsService.exists(queryWrapper);
        return R.success().setData(exists);
    }
}